VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsZLib"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
'-----------------------------------------------------------------
'             PT DC Hub @ Direct Connect P2P Network
'-----------------------------------------------------------------
'       Developer: Carlos.DF (fLaSh) - Portugal
'          E-mail: carlosferreiracarlos@hotmail.com
' Project started: 10 - September - 2006
'         License: GNU General Public License.
'-----------------------------------------------------------------
'       Thanks to developers and contributores of SDCH/DDCH
'         The Left Hand, ButterflySoul, HaArD and Selyb
'  TheNOP, RollTheDice, JDommi, GhOstFaCE, ArchaicLight and TUFF
'-----------------------------------------------------------------
Option Explicit

Private Declare Function Compress Lib "ZLIB.DLL" Alias "compress" (ByVal compr As String, ByRef comprLen As Any, ByVal buf As String, ByVal buflen As Long) As Long
Private Declare Function Decompress Lib "ZLIB.DLL" Alias "uncompress" (ByVal uncompr As String, ByRef uncomprLen As Any, ByVal compr As String, ByVal lcompr As Long) As Long

Public Function CompressString(ByVal sData, Optional ByVal bWithDescSizeID As Boolean = False) As String
1:    Dim sCompressed     As String
2:    Dim lCompressedLen  As Long
3:    Dim lStringLen      As String
4:    Dim lReturn         As Long
5:    On Error GoTo Err

7:    lStringLen = Len(sData)
8:    lCompressedLen = (lStringLen * 1.01) + 13
9:    sCompressed = Space(lCompressedLen)
    
11:   lReturn = Compress(sCompressed, lCompressedLen, sData, lStringLen)
    
       'Returns params:
       '   -6 = VERSION_ERROR
       '   -5 = BUF ERROR
       '   -4 = MEM ERROR
       '   -3 = DATA ERROR
       '   -2 = STREAM ERROR
       '   -1 = ERR NO
       '   -0 = OK
       '    1 = STREAM END
       '    2 = NEED DICT
23:    If lReturn = 0 Then
24:         sCompressed = Left(sCompressed, lCompressedLen)
25:         If bWithDescSizeID Then
                'Put the uncompressed length identifier in front of compressed data
                'easyer on ressources to uncompressed data if known.
28:             CompressString = lStringLen & ":" & sCompressed
29:         Else
                'In some cases might not be available when receiving, external to the hub, data...
31:             CompressString = sCompressed
32:         End If
33:    Else
34:         CompressString = vbNullString
35:    End If

37:    Exit Function
38:
Err:
39:    HandleError Err.Number, Err.Description, Erl & "|" & "clsZLib.CompressString()"
End Function

Public Function DecompressString(ByVal sData As String) As String
1:    Dim sUncompressedString As String
2:    Dim lUncompressedLen    As Long
3:    Dim sBuffer             As String
4:    Dim lBufferLen          As Long
5:    Dim lColonPosition      As Long
6:    Dim lReturn             As Long
7:    On Error GoTo Err
    
9:    lUncompressedLen = Val(sData)
10:   sUncompressedString = Space(lUncompressedLen)
    
12:   lColonPosition = Len(CStr(lUncompressedLen)) + 1
13:   sBuffer = Mid(sData, lColonPosition + 1)
14:   lBufferLen = Len(sBuffer)
    
16:   lReturn = Decompress(sUncompressedString, lUncompressedLen, sBuffer, lBufferLen)
    
       'Returns params:
       '   -6 = VERSION_ERROR
       '   -5 = BUF ERROR
       '   -4 = MEM ERROR
       '   -3 = DATA ERROR
       '   -2 = STREAM ERROR
       '   -1 = ERR NO
       '   -0 = OK
       '    1 = STREAM END
       '    2 = NEED DICT
28:    If lReturn = 0 Then
29:        DecompressString = sUncompressedString
30:    Else
31:        DecompressString = vbNullString
32:    End If
 
34:    Exit Function
35:
Err:
36:    HandleError Err.Number, Err.Description, Erl & "|" & "clsZLib.DecompressString()"
End Function
 
Public Sub CompressFile(ByRef strInput As String, ByRef strOutput As String)
1:    On Error GoTo Err
      'I could make it slightly faster, and might do so later
      'However this is quite acceptable right now
4:    g_objFileAccess.WriteFile strOutput, CompressString(g_objFileAccess.ReadFile(strInput), True)
5:    Exit Sub
6:
Err:
7:    HandleError Err.Number, Err.Description, Erl & "|" & "clsZLib.CompressFile(" & strInput & "; " & strOutput & ")"
End Sub

Public Sub DecompressFile(ByRef strInput As String, ByRef strOutput As String)
1:    On Error GoTo Err
      'I could make it slightly faster, and might do so later
      'However this is quite acceptable right now
4:    g_objFileAccess.WriteFile strOutput, DecompressString(g_objFileAccess.ReadFile(strInput))
    
6:    Exit Sub
7:
Err:
8:    HandleError Err.Number, Err.Description, Erl & "|" & "clsZLib.DecompressFile(" & strInput & "; " & strOutput & ")"
End Sub
